package com.crm.services;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.model.jdao.IOrderInfoDao;
import com.crm.model.mdao.CustomerMapper;
import com.crm.model.mdao.DictionariesMapper;
import com.crm.model.mdao.OrderInfoMapper;
import com.crm.model.mpojo.Customer;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.Timeline;
import com.github.pagehelper.PageHelper;

/**
 * 订单业务类
 * 
 * @author 周远星
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderInfoServers {
	@Autowired
	IOrderInfoDao iOrder;
	@Autowired
	OrderInfoMapper ordermaper;

	@Autowired
	DictionariesMapper dicmapper;
	@Autowired
	CustomerMapper customerMapper;
	
	@Autowired
	TimeLineServices timeLineServices;

	public List<Orderinfo> findAll() {
		return iOrder.findAll();
	}

	public List<Map<String, String>> findAllMaper() {
		return ordermaper.selectAll();
	}

	//获取当然订单数
	public int selectOrderNum() {
		return ordermaper.selectOrderNum();
	}
	
	/**
	 * 分页查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectByPager(int pageNum, int pageSize,Integer statusid,String search) {
		// 使用分页插件
		System.out.println("pageNum:" + pageNum + ",pageSize:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		Integer salesreturn=null;
		Integer dstatus=null;
		
		if(statusid!=null) {
		if(statusid==1098||statusid==1099||statusid==1100)
			dstatus=statusid;
		if(statusid==1095||statusid==1096)
			salesreturn=statusid;
		}
		// 获取
		List<Map<String, String>> list = ordermaper.selectAlls(salesreturn,dstatus,search);
		System.out.println(list);
		System.out.println("大小" + list.size());
		return list;
	}

	/**
	 * 查询发货状态
	 * 
	 * @return
	 */
	public List<Map<String, String>> selectFtype() {
		return dicmapper.selectByDicpar(1091);
	}

	/**
	 * 查询退货状态
	 * 
	 * @return
	 */
	public List<Map<String, String>> selectTtype() {
		return dicmapper.selectByDicpar(1094);
	}

	/**
	 * 查询订单状态
	 * 
	 * @return
	 */
	public List<Map<String, String>> selectOrdertype() {
		return dicmapper.selectByDicpar(1254);
	}

	/**
	 * 查询付款状态
	 * 
	 * @return
	 */
	public List<Map<String, String>> selectOrderpay() {
		return dicmapper.selectByDicpar(1085);
	}

	/**
	 * 添加订单
	 */
	public Orderinfo addOrder(Orderinfo orderinfo) {
		Orderinfo save = iOrder.save(orderinfo);
		Timeline timeline=new Timeline();
		timeline.setTheme("新建订单");
		timeline.setTimes(new Date());
		timeline.setCustId(orderinfo.getCustomer().getCustId());
		timeline.setDetails("单号:"+save.getOId()+"        主题:"+orderinfo.getOTitle()+"的订单");
		timeLineServices.insertTimeLine(timeline);
		System.out.println(save);
		//修改客户的类型为签约客户
		Customer cust=new Customer();
		cust.setCustId(orderinfo.getCustomer().getCustId());
		cust.setCustSort(1112);
		customerMapper.updateByPrimaryKeySelective(cust);
		return save;
	}

	/**
	 * 逻辑删除订单，将状态改为1103
	 */
	public Orderinfo deleteOrder(Integer id) {
		Orderinfo orderinfo = iOrder.findById(id).get();
		orderinfo.setOStatus(1103);
		Orderinfo save = iOrder.save(orderinfo);
		System.out.println(save);
		return save;
	}

	public List<Map<String, String>> selectAllBypageID(Integer cumid) {
		return ordermaper.selectAllBypageID(cumid);
	}

	/**
	 * 根据ID查询单条JPA
	 * 
	 * @param oid
	 * @return
	 */
	public Orderinfo findById(Integer oid) {
		return iOrder.findById(oid).get();
	}

	/**
	 * 根据ID查询用于前台订单视图展示
	 * 
	 * @param oid
	 * @return
	 */
	public Map<String, String> selectAllView(Integer oid) {
		return ordermaper.selectAllView(oid);
	}

	/**
	 * 根据ID查询当条订单信息
	 * 
	 * @param oid
	 * @return
	 */
	public Map<String, String> selectAllOrderByid(Integer oid) {
		return ordermaper.selectAllOrderByid(oid);
	}
	
	/**
	 * 根据ID查询当条订单信息
	 * 
	 * @param oid
	 * @return
	 */
	public List<Map<String, String>> selectAllBycustIDTwo(Integer oid) {
		return ordermaper.selectAllBycustIDTwo(oid);
	}
	
	/**
	 * 查询客户id订单状态为执行中
	 * @return 
	 */
	public List<Map<String, String>> selelctCustid(Integer custid) {
		return ordermaper.selectCustid(custid);
	}

}
